<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/13
 * Time: 11:03
 */

namespace app\flower\controller;
use cmf\controller\AdminBaseController;
use app\flower\model\FlowerOrdersModel;
use app\flower\model\FlowerCommentModel;
use think\Request;
use think\Db;
class AdminOrderController extends AdminBaseController
{
    protected $order;
    protected $comment;
    protected $statuss = array('1'=>'已经下单未支付','2'=>'已经支付还没有完成','3'=>'已经完成还未评价','4'=>'已经评价','5'=>'已发完');
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->order = new FlowerOrdersModel();
        $this->comment = new FlowerCommentModel();
    }

    /**
     * @return mixed
     * 订单列表页面
     */
    public function index(){

        $result = $this->order->where('deltime',0)->order('id desc')->paginate(8);
        $page = $result->render();
        $this->assign('orders',$result);
        $this->assign('page',$page);
        return $this->fetch('order');
     }

    /**
     * 内容评论
     */
    public function comment(){
        $result = $this->comment->where('deltime',0)->order('id desc')->paginate(8);
        $page = $result->render();
        $this->assign('orders',$result);
        $this->assign('page',$page);
        return $this->fetch('comment');
    }

    /**
     * @return mixed
     * 根据订单类型，或则时间段，或则关键词来搜索
     */
    public function searchorder(){
        $ordersql = $this->order;
        $orderstatus = input('orderstatus');
        if($orderstatus!='all'){
            $ordersql = $ordersql->where('status',$orderstatus);
        }
        $start_time = input('start_time');//开始时间
        $end_time = input('end_time');//结束时间
        if($start_time!=null&&$end_time!=null){
            $ordersql = $ordersql->where('stime','>=',strtotime($start_time))->where('stime','<=',strtotime($end_time));
        }else if($start_time!=null&&$end_time==null){
            $this->error('请选择结束时间');
        }else if($start_time==null&&$end_time!=null){
            $this->error('请选择开始时间');
        }
        $keyword = input('keyword');//关键词
        if($keyword!=null){
            $ordersql = $ordersql->where('order|weixinorders|userid|phone','like','%'.trim($keyword).'%');
        }
        $result = $ordersql->paginate(8);

        $page = $result->render();
        $this->assign('orders',$result);
        $this->assign('page',$page);
        return $this->fetch('order');
    }
    /**
     * @return mixed
     * 根据评论的分值，或则时间段，或则关键词来搜索
     */
    public function searchcomment(){
        $ordersql = $this->comment;
        $orderstatus = input('commentvalue');
        if($orderstatus!='all'){
            $ordersql = $ordersql->where('value',$orderstatus);
        }
        $start_time = input('start_time');//开始时间
        $end_time = input('end_time');//结束时间
        if($start_time!=null&&$end_time!=null){
            $ordersql = $ordersql->where('dottime','>=',strtotime($start_time))->where('dottime','<=',strtotime($end_time));
        }else if($start_time!=null&&$end_time==null){
            $this->error('请选择结束时间');
        }else if($start_time==null&&$end_time!=null){
            $this->error('请选择开始时间');
        }
        $keyword = input('keyword');//关键词
        if($keyword!=null){
            $ordersql = $ordersql->where('orderid|userid','like','%'.trim($keyword).'%');
        }
        $result = $ordersql->paginate(8);
        $page = $result->render();
        $this->assign('orders',$result);
        $this->assign('page',$page);
        return $this->fetch('comment');
    }

    /**
     *删除订单管理
     */
    public function deleteorder(){
          $param  = $this->request->param();
          if(isset($param['id'])){
              $id = $this->request->param('id');
              $result= $this->order->get($id);
              $order = $result['order'];
              $res =  $this->comment->where('orderid',$order)->count();
              if($res>0){
                  $this->error('该订单已经被评论，请先删除评论');
                  return;
              }
              $time = strtotime($result['stime']);
              $date=floor((strtotime(date("y-m-d h:i:s"))-$time)/86400);
             /* if($date<=365){
                  $this->error('该订单还没有过一年，无法删除');
                  return;
              }*/
              $result = $this->order->where('id',$id)->update(array('deltime'=>time()));
              if($result>0){
                  $this->success('删除成功');
              }else{
                  $this->success('删除失败');
              }
          }

        //=============================批量删除=============================
        if(isset($param['ids'])){
           // print_r($param);exit;
             $ids = $this->request->param('ids/a');//转成数组
            // print_r($ids);exit;
             foreach($ids as $k=>$v){
                //$this->commondelorder($v);
             }
            $result = $this->order->where(array('id'=>array('in',$ids)))->update(array('deltime'=>time()));
            //echo $this->order->getLastSql();exit;
            if($result>0){
                $this->success('删除成功');
            }else{
                $this->success('删除失败');
            }
        }
    }
    function commondelorder($id){
        $result= $this->order->get($id);
        $order = $result['order'];
        $res =  $this->comment->where('orderid',$order)->count();
        if($res>0){
            $this->error('订单'.$order.'已经被评论，请先删除评论');
            return;
        }
        $time = strtotime($result['stime']);
        $date=floor((strtotime(date("y-m-d h:i:s"))-$time)/86400);
        if($date<=365){
            $this->error('订单'.$order.'还没有过一年，无法删除');
            return;
        }
    }
    /**
     * 删除评论的内容
     */
    public function deletecomment(){
        $param  = $this->request->param();
        if(isset($param['id'])){
            $id = $param['id'];
            $result = $this->comment->where('id',$id)->update(array('deltime'=>time()));
            if($result>0){
                $this->success('删除成功');
            }else{
                $this->success('删除失败');
            }
        }
        //=============================批量删除=============================
        if(isset($param['ids'])){
            $ids = $this->request->param('ids/a');//转成数组
            $result = $this->comment->where(array('id'=>array('in',$ids)))->update(array('deltime'=>time()));
            if($result>0){
                $this->success('删除成功');
            }else{
                $this->success('删除失败');
            }
        }

    }

    /**
     * 修改备注
     */
    public function remarks(){
         $id = input('id');
        $result = Db::name('flower_orders')->where('id',$id)->find();

        $this->assign('order',$result);
        $this->assign('statuss',$this->statuss);
         return $this->fetch('remarks');
    }

    /**
     * 修改备注
     */
    public function setRemarks(){
         $id = input('id');
         $remarks = input('remarks');
         $address = input('address');
         $data = [];
        $data['remarks'] = $remarks;
        $data['address'] = $address;
        $result = $this->order->where('id',$id)->update($data);
        if(!empty($result)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function ordercomplete(){
        $id = input('id');
        $data = [];
        $result2 = Db::name('flower_orders')->where('id',$id)->find();
         if($result2['status'] <=1){
             $this->error('该订单支付未完成，无法修改订单！');
         }else{
             $data['status']= 3;
         }
        $result = $this->order->where('id',$id)->update($data);
        //echo $this->order->getLastSql();exit;
        if(!empty($result)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
     /**
     * 导出excel
     */
    public function outputexcel(){
        $excel = new \PHPExcel();//创建PHPExcel实例

        $excel->setActiveSheetIndex(0)//设置表头
            ->setCellValue('A1', '订单编号')
            ->setCellValue('B1', '用户id')
            ->setCellValue('C1', '下单时间')
            ->setCellValue('D1', '支付时间')
            ->setCellValue('E1', '完成时间')
            ->setCellValue('F1', '订单截至时间')
            ->setCellValue('G1', '用户手机号')
            ->setCellValue('H1', '订单具体内容')
            ->setCellValue('I1', '提货地点以及时间')
            ->setCellValue('J1', '备注')
            ->setCellValue('K1', '状态');
        $ordersql = $this->order;
        $orderstatus = input('orderstatus');
        if($orderstatus!='all'){
            $ordersql = $ordersql->where('status',$orderstatus);
        }
        $start_time = input('start_time');//开始时间
        $end_time = input('end_time');//结束时间
        if($start_time!=null&&$end_time!=null){
            $ordersql = $ordersql->where('stime','>=',strtotime($start_time))->where('stime','<=',strtotime($end_time));
        }else if($start_time!=null&&$end_time==null){
            $this->error('请选择结束时间');
        }else if($start_time==null&&$end_time!=null){
            $this->error('请选择开始时间');
        }
        $keyword = input('keyword');//关键词
        if($keyword!=null){
            $ordersql = $ordersql->where('order|weixinorders|userid|phone','like','%'.trim($keyword).'%');
        }
        $result = $ordersql->order('id','desc')->where('deltime','0')->select();
        $result = json_encode($result);
        $result = json_decode($result);

        for($i=0;$i<count($result);$i++){
            $excel->getActiveSheet()
                ->setCellValue("A".($i+2), $result[$i]->order)
                ->setCellValue("B".($i+2), $result[$i]->userid)
                ->setCellValue("C".($i+2), $result[$i]->stime)
                ->setCellValue("D".($i+2), $result[$i]->paytime)
                ->setCellValue("E".($i+2), $result[$i]->etime)
                ->setCellValue("F".($i+2), $result[$i]->endtime)
                ->setCellValue("G".($i+2), $result[$i]->phone)
                ->setCellValue('H'.($i+2), $result[$i]->content)
                ->setCellValue('I'.($i+2), $result[$i]->address)
                ->setCellValue('J'.($i+2), $result[$i]->remarks)
                ->setCellValue('K'.($i+2), $result[$i]->status);
            $excel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
            $excel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
            $excel->getActiveSheet()->getColumnDimension('H')->setWidth(35);
            $excel->getActiveSheet()->getColumnDimension('I')->setWidth(60);
            $excel->getActiveSheet()->getColumnDimension('J')->setWidth(35);
            $excel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
        }
        $fileName="客户订单数据";

        ob_end_clean();//清除缓冲区,避免乱码
        $fileName = iconv("utf-8", "gb2312", $fileName);

        //重命名表
        $excel->getActiveSheet()->setTitle('客户订单数据');
       //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $excel->setActiveSheetIndex(0);

        //将输出重定向到一个客户端web浏览器(Excel2007)
         header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
         header('Content-Disposition: attachment;filename="'.$fileName.(date('Ymd-H:i:s ')).'.xls"');
         header('Cache-Control: max-age=1');

         $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');//Excel5为xls格式，excel2007为xlsx格式
         $objWriter->save('php://output'); //文件通过浏览器下载

    }


}